package com.lyqg.biz;

import static com.core.Constants.STATUS_NORMAL;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.core.orm.Page;
import com.core.orm.PageRequest;
import com.lyqg.dao.JyDao;
import com.lyqg.domain.Jy;

@Service
@Transactional
public class JyService extends BaseService implements IJyService {
	String hql = "FROM Jy x WHERE status=?";

	@Resource
	JyDao jyDao;

	@Override
	public Page<Jy> queryJy(PageRequest pageRequest) {
		return jyDao.findPage(pageRequest, hql, STATUS_NORMAL);
	}

	@Override
	public List<Jy> queryLimitJy(int limit) {
		String hql = "FROM Jy WHERE status=? ORDER BY id DESC";
		return jyDao.findLimit(hql, limit, STATUS_NORMAL);
	}

	@Override
	public List<Jy> queryLimitJyByArea(int limit, String area) {
		String hql = "FROM Jy WHERE status=? AND areaEname=? ORDER BY modifyTime DESC";
		return jyDao.findLimit(hql, limit, STATUS_NORMAL, area);
	}

	@Override
	public Page<Jy> queryJyByCate(PageRequest pageRequest,
			String categoryParentEname) {
		String whereHql = " AND categoryParentEname=?";
		return jyDao.findPage(pageRequest, hql + whereHql, STATUS_NORMAL,
				categoryParentEname);
	}

	@Override
	public Page<Jy> queryJyByArea(PageRequest pageRequest, String areaEname) {
		String whereHql = " AND areaEname=?";
		return jyDao.findPage(pageRequest, hql + whereHql, STATUS_NORMAL,
				areaEname);
	}

	@Override
	public Jy getById(int id) {
		return jyDao.get(id);
	}

	@Override
	public void save(Jy jy) {
		jyDao.save(jy);
	}

	@Override
	public int updatePingFen(int contentId) {
		String hql = "UPDATE Jy SET zhiCount=zhiCount+1 WHERE id=?";
		return jyDao.batchExecute(hql, contentId);
	}

	@Override
	public int updateCollectionCount(int id) {
		String hql = "UPDATE Jy SET collectionCount=collectionCount+1 WHERE id=?";
		return jyDao.batchExecute(hql, id);
	}

	@Override
	public int updateCommentCount(int id) {
		String hql = "UPDATE Jy SET commentCount=commentCount+1 WHERE id=?";
		return jyDao.batchExecute(hql, id);
	}

	@Override
	public Long queryNumByUser(int userId) {
		String sql = "SELECT COUNT(*) FROM tbl_jy WHERE user_id=? AND status=?";
		return jdbcTemplate.queryForLong(sql, userId, STATUS_NORMAL);
	}

	@Override
	public Page<Jy> queryJyByUser(PageRequest pageRequest, int userId) {
		String hql = "FROM Jy x WHERE userId=?";
		return jyDao.findPage(pageRequest, hql, userId);
	}

	@Override
	public Page<Jy> queryJyByUserAndStatus(PageRequest pageRequest, int userId,
			int status) {
		String hql = "FROM Jy x WHERE userId=? AND status=?";
		return jyDao.findPage(pageRequest, hql, userId, STATUS_NORMAL);
	}
}
